﻿
namespace SC2BuildPlannerLib.Resources
{
    /// <summary>
    /// Class for all resource data (minerals, gas, supply, energy, larvae)
    /// </summary>
    public class Resource
    {
        /// <summary>
        /// Property for the initial amount of the resource
        /// </summary>
        private int _initialAmount;
        public int InitialAmount
        {
            get { return _initialAmount; }
            set { _initialAmount = value; }
        }

        /// <summary>
        /// Property for the current amount of the resource
        /// </summary>
        private virtual int _amount;
        public virtual int Amount
        {
            get { return _amount; }
            set { _amount = value; }
        }

        /// <summary>
        /// Property for the maximum value of the resource
        /// </summary>
        private int? _maximum;
        public int? Maximum
        {
            get { return _maximum; }
            set { _maximum = value; }
        }

        /// <summary>
        /// Basic constructor
        /// </summary>
        public Resource()
        {
        }

        /// <summary>
        /// Constructor with parameter for setting the initial amount
        /// </summary>
        /// <param name="amount">Amount</param>
        public Resource(int amount) : this(amount, null)
        {
        }

        /// <summary>
        /// Constructor with parameters for setting the inital amount and maximum
        /// </summary>
        /// <param name="amount">Amount</param>
        /// <param name="maximum">Maximum</param>
        public Resource(int amount, int? maximum)
        {
            InitialAmount = amount;
            Maximum = maximum;
            Reset();
        }

        /// <summary>
        /// Virtual method for checking if a resource change is possible
        /// </summary>
        /// <param name="delta">Delta amount</param>
        /// <returns>True if the Amount is greater than or equal to 0 after the delta, otherwise false</returns>
        public virtual bool CanChange(int delta)
        {
            return Amount + delta >= 0;
        }

        /// <summary>
        /// Virtual method for changing the amount of the resource
        /// </summary>
        /// <param name="delta">Delta amount</param>
        public virtual void Change(int delta)
        {
            if (Maximum != null && Amount + delta >= Maximum.Value)
                Amount = Maximum.Value;
            else
                Amount += delta;
        }

        /// <summary>
        /// Virtual method for resetting the resource to its initial state
        /// </summary>
        public virtual void Reset()
        {
            Amount = InitialAmount;
        }
    }
}
